import frpOption from "../option.json" with { type: "json" }
import logger from "./logger.js"

// import { getStorageKey, localStorage, StorageKeyType } from "./localStorage.js"


async function kingdeeStartWorking(orderCode, productionOrderList) {
    // const storageKey = getStorageKey(orderCode, StorageKeyType.syncKingdeeOrder)
    // let syncKingdeeOrderStatus = JSON.parse(localStorage.getItem(storageKey))
    // if (syncKingdeeOrderStatus == null) {
    //     syncKingdeeOrderStatus = []
    // }
    // const runProductionList = productionOrderList.filter(item => !syncKingdeeOrderStatus.includes(item.code))

    // console.log(JSON.stringify(productionOrderList))

    const jFidList = productionOrderList.map(item => item.j_FID)
    const syncRes = await fetch(`${frpOption.api.kingdeeApi}`, {
        method: "post",
        body: JSON.stringify({
            fn: 'ExcuteOperation',
            param: {
                formid: 'PRD_MO',
                opNumber: 'ToStart',
                data: {
                    Ids: jFidList.join(',')
                }
            }
        }),
    }).then((res) => res.json())
    if (syncRes?.res?.Result?.ResponseStatus?.MsgCode !== 0) {
        throw new Error("金蝶执行至开工失败：" + JSON.stringify(syncRes))
    } else {
        logger.info(`${orderCode} 金蝶执行至开工成功`)
    }
}

export { kingdeeStartWorking }